package y23.Sep;

public class Test13 {
    public static void main(String[] args) {
        Solution6 solution6 = new Solution6();
        String[] words1 = {"cat","bt","hat","tree"};
        System.out.println(solution6.countCharacters(words1,"atach"));
        String[] words2 = {"hello","world","leetcode"};
        System.out.println(solution6.countCharacters(words1,"awelldonehoneyr"));
    }
}

class Solution6 {
    public int countCharacters(String[] words, String chars) {
        int[] charsCount = letter(chars);
        int sum = 0;
        for(int i = 0;i < words.length;i++){
            int count = 0;
            int[] word = letter(words[i]);
            for (int j = 0; j < 26; j++) {
                if(word[j] > charsCount[j]){
                    count++;
                }
            }
            if (count == 0){
                sum += words[i].length();
            }
        }
        return sum;
    }
    public int[] letter(String word) {
        int[] counter = new int[26];
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            counter[c-'a']++;
        }
        return counter;
    }
}


